using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using NCS.Module.CMS.Library.Distributions;
using NCS.Module.CMS.Library.Definitions;
using NCS.Module.CMS.Library.Zones;
using NCS.Library.Utilities;
using NCS.Module.CMS.Library;
using System.Xml;
using System.Text.RegularExpressions;
using System.Text;

namespace NCS.Module.CMS.Web.HomePage
{
    public partial class DetailRss : CMSWebControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (curZone == null) return;
            // read
            Response.Clear();

            Response.ContentType = "text/xml";

            XmlTextWriter xtwFeed = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);

            xtwFeed.WriteStartDocument();
            // The mandatory rss tag
            xtwFeed.WriteStartElement("rss");
            xtwFeed.WriteAttributeString("version", "2.0");
            // The channel tag contains RSS feed details
            xtwFeed.WriteStartElement("channel");

            xtwFeed.WriteElementString("title", "cuc ung dung CNTT Bo TTTT - Rss Feed");

            xtwFeed.WriteElementString("link", "http://ict.gov.vn");

            xtwFeed.WriteElementString("description", "The latest news from all category of ict.gov.vn.");

            xtwFeed.WriteElementString("copyright", "Copyright 2008  by ict.gov.vn. All rights reserved.");

            xtwFeed.WriteElementString("author", "Hoang Xuan Nguyen");
            // Objects needed for connecting to the SQL database 
            DataTable td = null;
            td = DistributionController.GetDistributedContentsByZone(curZone.Zone_ID);
            foreach (DataRow dr in td.Rows)
            {
                xtwFeed.WriteStartElement("item");

                xtwFeed.WriteElementString("title", dr["Content_Headline"].ToString());

                xtwFeed.WriteElementString("description", ReplateHtml(dr["Content_Teaser"].ToString()));

                xtwFeed.WriteElementString("link", "http://" + BuildUrlByDistId((int)dr["Content_ID"], this.curZone.Zone_ID) + "");

                xtwFeed.WriteElementString("pubDate", dr["Content_CreateDate"].ToString());

                xtwFeed.WriteEndElement();
            }
            // Close all tags 

            xtwFeed.WriteEndElement();

            xtwFeed.WriteEndElement();

            xtwFeed.WriteEndDocument();

            xtwFeed.Flush();

            xtwFeed.Close();

            Response.End();
        }
        private string ReplateHtml(string str)
        {
            return Regex.Replace(str, "<[a-zA-Z/][^>]*>", "").ToString();
        }
    }
}